﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Cart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        DTO.GioHangDto GioHang = (DTO.GioHangDto)Session["GioHang"];
        DTO.NguoiDungDto user = (DTO.NguoiDungDto)Session["user"];

        if (GioHang == null)
            GioHang = new DTO.GioHangDto();

        if (user == null)
        {
            GioHang.IDNguoiDung = -1;
            // <NEW CODE>
            if (GioHang != null)
            {
                for (int j = 0; j < GioHang.lstHang.Count; j++)
                {
                    GioHang.lstHang[j].RateGiamGia = 0;
                }
            }
            // </NEW CODE>
        }
        else
        {
            GioHang.IDNguoiDung = user.Ma;
            //<NEW CODE>
            if (GioHang != null)
            {
                for (int j = 0; j < GioHang.lstHang.Count; j++)
                {
                    GioHang.lstHang[j].RateGiamGia = BUS.GiamGiaBus.LayTiLeGiamGia(user.Diem);
                }
            }
            //</NEW CODE>
        }

        String itemID = Request["itemID"];
        if (itemID == null)
        {
            if(GioHang.lstHang.Count == 0)
                Session["GioHang"] = null;
            return;
        }

        // Xu ly them hang HERE
        DTO.HangDto hangTemp = new DTO.HangDto();
        int temp = Int16.Parse(itemID);

        //Tim xem san pham co trong gio hang chua, neu co roi` thi tang so luong
        for (int i = 0; i < GioHang.lstHang.Count; i++)
            if (GioHang.lstHang[i].ID == temp)
            {
                DTO.SanPhamDto sanphamTemp = BUS.SanPhamBus.LaySanPhamTheoID(GioHang.lstHang[i].ID);
                GioHang.lstHang[i].SoLuong++;
                if (GioHang.lstHang[i].SoLuong > sanphamTemp.SoLuongCon)
                    GioHang.lstHang[i].SoLuong = sanphamTemp.SoLuongCon;
                Session["GioHang"] = GioHang;
                return;
            }

        DTO.SanPhamDto SanPham = BUS.SanPhamBus.LaySanPhamTheoID(temp);
        GioHang.Ngay = DateTime.Now;
        hangTemp.ID = SanPham.ID;
        hangTemp.UrlHinh = SanPham.Hinh;
        hangTemp.TenSanPham = SanPham.TenSanPham;
        hangTemp.DonGia = SanPham.DonGia;
        hangTemp.SoLuong = 1;
        //<NEW CODE>
        // Lay hinh thuc khuyen mai ap dung cho san pham
        hangTemp.RateKhuyenMai = BUS.KhuyenMaiBus.LayTiLeKhuyenMaiTheoID(SanPham.IDKhuyenMai);
        //Xet xem nguoi dung co duoc giam gia hay ko
        if (user == null)
        {
            hangTemp.RateGiamGia = 0;
        }
        else
        {
            if (SanPham.IDGiamGia == 0)
                hangTemp.RateGiamGia = 0;
            else
                hangTemp.RateGiamGia = BUS.GiamGiaBus.LayTiLeGiamGia(user.Diem);
        }
        //</NEW CODE>
        GioHang.lstHang.Add(hangTemp);

        Session["GioHang"] = GioHang;
    }
}
